﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Text;
/*
    2012-2-17 David检查正常
 */
namespace BenefitHelper.Sys
{
    /// <summary>
    /// 每天都会记录一下交易员是哪个组的
    /// </summary>
    public class DayGroupOperators
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 小组编号
        /// </summary>
        public int GroupId { get; set; }
        /// <summary>
        /// 小组
        /// </summary>
        public virtual Groups Group { get; set; }
        /// <summary>
        /// 操作员编号
        /// </summary>
        public int OperatorId { get; set; }
        /// <summary>
        /// 操作员
        /// </summary>
        public virtual Operator Operator { get; set; }
        /// <summary>
        /// 交易日期
        /// </summary>
        public DateTime Pdate { get; set; }


        /// <summary>
        /// 得到指定日期的小组的组员
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public List<Operator> GetGroupOperators(int groupId, DateTime pdate, DB.DBManager db)
        {
            List<Operator> operators = new List<Operator>();
            var query = (from t in db.DayGroupOperators where t.GroupId == groupId && t.Pdate.Day == pdate.Day && t.Pdate.Month == pdate.Month && t.Pdate.Year == pdate.Year select t).Include(a => a.Operator).ToList();
            foreach (DayGroupOperators dayGO in query)
            {
                operators.Add(dayGO.Operator);
            }
            return operators;
        }
        /// <summary>
        /// 得到指定日期的所有组员
        /// </summary>
        /// <param name="groupId"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public List<DayGroupOperators> GetGroupOperators(DateTime pdate, DB.DBManager db)
        {
            List<DayGroupOperators> operators = new List<DayGroupOperators>();
            var query = (from t in db.DayGroupOperators where t.Pdate.Day == pdate.Day && t.Pdate.Month == pdate.Month && t.Pdate.Year == pdate.Year select t).ToList();
            foreach (DayGroupOperators dayGO in query)
            {
                operators.Add(dayGO);
            }
            return operators;
        }


        /// <summary>
        /// 得到操作员制定日期所在小组
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int GetGroupId(int operatorid, DateTime pdate, DB.DBManager db)
        {
            int gid = 0;
            var query = from t in db.DayGroupOperators where t.OperatorId == operatorid && t.Pdate.Day == pdate.Day && t.Pdate.Month == pdate.Month && t.Pdate.Year == pdate.Year select t;
            if (query.Count() > 0)
            {
                gid = query.First().GroupId;
            }
            return gid;
        }

        /// <summary>
        /// 得到操作员当月所在小组，如果交易员换组，则返回最后的组
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public int GetGroupIdInMonth(int operatorid, int month, int year, DB.DBManager db)
        {
            int gid = 0;
            var query = from t in db.DayGroupOperators where t.OperatorId == operatorid && t.Pdate.Month == month && t.Pdate.Year == year orderby t.Pdate.Day descending select t;
            if (query.Count() > 0)
            {
                gid = query.First().GroupId;
            }
            return gid;
        }

        /// <summary>
        /// 初始化每天的小组人员关系
        /// </summary>
        public void InitDayGroupOperators(DateTime pdate, DB.DBManager db)
        {
            var query = from t in db.GroupOperators.Include(a => a.Operator).Include(a => a.Group) select t;
            foreach (Sys.GroupOperators o in query)
            {
                if (!o.Group.IsDeleted && !o.Operator.IsDeleted)
                {
                    DayGroupOperators dgop = new DayGroupOperators();
                    dgop.Pdate = pdate;
                    dgop.GroupId = o.GroupId;
                    dgop.OperatorId = o.OperatorId;
                    db.DayGroupOperators.Add(dgop);
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 删除某天的小组人员关系
        /// </summary>
        public void DeleteDayGroupOperators(DateTime pdate, DB.DBManager db)
        {
            var query = from t in db.DayGroupOperators where t.Pdate.Year == pdate.Year && t.Pdate.Month == pdate.Month && t.Pdate.Day == pdate.Day select t;
            foreach (Sys.DayGroupOperators o in query)
            {
                db.DayGroupOperators.Remove(o);
            }
            db.SaveChanges();
        }



        public List<DayGroupOperators> GetList(DB.DBManager db)
        {
            Sys.TradeHistory th = new TradeHistory();
            var lastTradeDay = db.TradeHistorys.OrderByDescending(a => a.Id);
            th = lastTradeDay.First();
            if (lastTradeDay.Count() > 0)
            {
                return db.DayGroupOperators.Where(a => a.Pdate.Month == th.MPdate).Where(a => a.Pdate.Year == th.YPdate).Where(a => a.Pdate.Day == th.DPdate).Include(a => a.Group).ToList();
            }
            else
            {
                return null;
            }

        }
    }
}
